/*
 * Copyright 2008 The Brui Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
 
package com.googlecode.brui.beans;

/**
 * Translates between object values and string representations and checks if a
 * textual representation is valid for the type.
 * 
 * @param T the type of objects understood by this {@link Validator}.
 */
// TODO better name for this type
public interface Validator<T> {

	/**
	 * Check if some text represents a valid value for this {@link Validator}'s
	 * type.
	 * 
	 * @param text a textual value of the type understood by this
	 *        {@link Validator}.
	 * @return <code>true</code> if the text is a valid representation of an
	 *         instance of this {@link Validator}'s type.
	 */
	boolean isValid(String text);

	/**
	 * Get the object value of the given text.
	 * 
	 * @param text some textual value of an object.
	 * @return the object represented by the given text.
	 * @throws ValidationException if the text is not a valid representation of
	 *         an instance of this {@link Validator}'s type.
	 */
	T fromText(String text);

	/**
	 * Get a textual representation of an object.
	 * 
	 * @param object the object to get the text value of.
	 * @return a textual representation of the given object.
	 * @throws IllegalArgumentException if the given object is not an instance of
	 *         the type supported by this {@link Validator}.
	 */
	String asText(T object);

	/**
	 * @return the type of objects which get translated by instances of this
	 *         {@link Validator}.
	 */
	Class<T> getType();
}
